package dao

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

func (d *Dao) PopMsg(key string, f func(channel string, msg []byte)) error {

	c := d.messagechannel.GetConn()
	defer c.Close()
	channel := redis.PubSubConn{c}
	channel.Subscribe(key)

	for {
		v := channel.Receive()
		switch v.(type) {
		case redis.Message:
			f(v.(redis.Message).Channel, v.(redis.Message).Data)
		case redis.Subscription:
			fmt.Printf("%s: %s %d\n", v.(redis.Subscription).Channel, v.(redis.Subscription).Kind, v.(redis.Subscription).Count)
		case error:
			return nil
		}
	}
	return nil
}
